#include <twit/resource/twit4c/TwitUtil.h>
#include <string>
#include <time.h>

time_t TwitUtil_string_to_time_t(const char* timeStr)
{
	using namespace std;
	if(!timeStr) return 0;
	string s(timeStr);
	
	struct tm t={0,};

	// parse Time string ex. 
	static const char* month[] = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
	#define TO_TIME(b,l) atoi(s.substr(b,l).c_str())
	/*
	case 10: // trends/weekly  - ex. 2010-09-25
	case 16: // trends/daily   - ex. 2010-10-01 16:26
	case 19: // trehds/current - ex. 2010-09-01 23:56:17
	case 30: // utc            - ex. Sun Sep 19 05:35:04 +0000 2010
	case 31: // trends.as_of   - ex. Wed, 29 Sep 2010 04:41:09 +0000
	*/
	const size_t timeLen = s.size();
	switch(timeLen)
	{
	case 10: // trends/weekly  - ex. 2010-09-25
	case 16: // trends/daily   - ex. 2010-10-01 16:26
	case 19: // trehds/current - ex. 2010-09-01 23:56:17
		{
			t.tm_year   = TO_TIME(0,4) - 1900;
			t.tm_mon    = TO_TIME(5,2);
			t.tm_mday   = TO_TIME(8,2);

			if(timeLen>10)
			{
				t.tm_hour   = TO_TIME(11,2);
				t.tm_min    = TO_TIME(14,2);

				if(timeLen>16)
				{
					t.tm_sec = TO_TIME(17,2);
				}
			}
      }break;
   case 30: // utc            - ex. Sun Sep 19 05:35:04 +0000 2010
      {
         // month
         {
            std::string m = s.substr(4,3);
            for(int i=0; i<sizeof(month)/sizeof(month[0]); i++)
            {
               if(strcmp(month[i], m.c_str())==0)
               {
                  t.tm_mon = i;
                  break;
               }
            }
         }

         t.tm_mday   = TO_TIME(8,2);
         t.tm_hour   = TO_TIME(11,2);
         t.tm_min    = TO_TIME(14,2);
         t.tm_sec    = TO_TIME(17,2);
         t.tm_year   = TO_TIME(26,4) - 1900;
      }break;
   case 31: // trends.as_of   - ex. Wed, 29 Sep 2010 04:41:09 +0000
      {
         // month
         {
            string m = s.substr(8,3);
            for(int i=0; i<sizeof(month)/sizeof(month[0]); i++)
            {
               if(strcmp(month[i], m.c_str())==0 )
               {
                  t.tm_mon = i;
                  break;
               }
            }
         }

         t.tm_mday   = TO_TIME(5,2);
         t.tm_year   = TO_TIME(12,4)-1900;
         t.tm_hour   = TO_TIME(17,2);
         t.tm_min    = TO_TIME(20,2);
         t.tm_sec    = TO_TIME(23,2);
      }break;
	default:
      return 0;
	}
	return mktime(&t);
}

size_t TwitUtil_strftime(char* string, size_t maxsize, const char* format, const struct tm *timeptr)
{
	return strftime(string, maxsize, format, timeptr);
}

struct tm* TwitUtil_gmtime(const time_t* _Time)
{
	return gmtime(_Time);
}

void TwitUtil_rgb_to_string(unsigned char r, unsigned char g, unsigned char b, char* buf)
{
	sprintf(buf, "%02X%02X%02X", r, g, b);
}

void TwitUtil_locationValue_to_string(double val, char* buf)
{
	if(buf)
	{
		sprintf(buf, "%g", val);
	}
}